package geektime.spring.springbucks.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.mapper.OrderCoffeeMapper;
import geektime.spring.springbucks.mapper.OrderMapper;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.model.CoffeeExample;
import geektime.spring.springbucks.model.Order;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Pactera
 */
@RestController
@Slf4j
@RequestMapping("/coffee")
public class CoffeeController {

//    @Resource
//    StringRedisTemplate stringRedisTemplate;
//    @Autowired
//    private CoffeeMapper coffeeMapper;
//    @Autowired
//    private OrderMapper orderMapper;
//    @Autowired
//    private OrderCoffeeMapper orderCoffeeMapper;
//
//    private void queryCoffeeByIds() {
//
//        List<Long> ids = Arrays.asList(1L, 2L, 3L);
//        CoffeeExample coffeeExample = new CoffeeExample();
//        coffeeExample.createCriteria().andIdIn(ids);
//        List<Coffee> coffees = coffeeMapper.selectByExample(coffeeExample);
//        log.info("coffees is:{}", coffees);
//    }
//
//    private void getCoffeeByPage() {
////        参考：https://blog.csdn.net/weixin_44307065/article/details/109473284
//
////        PageHelper
//        PageHelper.startPage(1, 2);
//        CoffeeExample ce = new CoffeeExample();
//        List<Coffee> coffees = coffeeMapper.selectByExample(ce);
//        log.info("getCoffeeByPage is:{}", coffees);
//        PageInfo<Coffee> coffeePageInfo = new PageInfo<>(coffees);
//        log.info("page info is:{}", coffeePageInfo);
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    private void deleteOrder() {
//        int i = orderMapper.deleteByPrimaryKey(1L);
//        log.info("影响行数：{}", i);
//    }
//
//    //
//    @Transactional(rollbackFor = Exception.class)
//    private void addOrder() {
//        Order order = new Order().withCustomer("小张").withCreateTime(new Date()).withUpdateTime(new Date()).withState(1);
//        System.out.println("------order is:---------");
//        System.out.println(order);
//        orderMapper.insertSelective(order);
//        System.out.println(order);
//    }
//
//    @Transactional(rollbackFor = Exception.class)
//    private void updateCoffee() {
//        Coffee coffee = coffeeMapper.selectByPrimaryKey(1L);
//        coffee.setName("name for test");
//        coffeeMapper.updateByPrimaryKeySelective(coffee);
//        log.info("coffee update to:{}", coffee);
//    }

    @Autowired
    CoffeeService coffeeService;

    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    public Coffee queryCoffee(@PathVariable("id") Long id) {
        Coffee s = coffeeService.getCoffee(id);
        return s;
    }



    @GetMapping(value = "/xml/{id}",produces = MediaType.APPLICATION_XML_VALUE)
    public Coffee queryCoffeeForXml(@PathVariable("id") Long id) {
        Coffee s = coffeeService.getCoffee(id);
        return s;
    }
}
